package cn.gok.fangxin.base.web;

import cn.gok.fangxin.base.entity.BaseEntity;
import cn.gok.fangxin.base.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Date;

/**
 * ClassName : cn.edu.qzit.linpq.base.web.BaseController
 * Desc : BaseController 用于规范所有的Controller基础方法的实现
 *
 * @author : LINPQ
 * @date : 2022/6/18 14:42
 */
public abstract class BaseController<S extends BaseService,T extends BaseEntity> {
    @Autowired
    S service;

    public abstract String getPerfix();


    @PostMapping("add")//创建时间
    public String add(T entity){
        entity.setCreateTime(new Date());
        entity.setUpdateTime(new Date());
        service.save(entity);
        return "redirect:"+getPerfix();
    }



    @PostMapping("edit")
    public String edit(T entity){
        entity.setUpdateTime(new Date());
        service.update(entity);
        return "redirect:"+getPerfix();
    }

//    @GetMapping("delete/{id}")
//    public String delete(@PathVariable("id") Integer id){
//        service.delete(id);
//        return "redirect:"+getPerfix();
//    }
    @RequestMapping("delete")
    public String delete(T entity){
        service.delete(entity.getId());
        return "redirect:"+getPerfix();
    }
    @RequestMapping("list")
    public void list(T entity){
        service.list();
    }
}
